import Cookies from 'js-cookie'
import clearAllCookie from '../../utils/CookieUtil'

const state = {
    signature: Cookies.get('signature'),
    nickName: Cookies.get('nickName'),
    avatar: Cookies.get('avatar'),
}

const getters = {
    getSignature() {
        return state.signature;
    },
    getNickName() {
        return state.nickName;
    },
    getAvatar() {
        return state.avatar;
    },
    getUserInformation(){
        return {
            signature : state.signature,
            nickName : state.nickName,
            avatar : state.avatar,
        }
    }
}

const mutations = {
    SET_SIGNATURE: (state, signature) => {
        state.signature = signature;
        Cookies.set('signature', signature);
    },
    SET_NICKNAME: (state, nickName) => {
        state.nickName = nickName;
        Cookies.set('nickName', nickName);
    },
    LOGOUT: (state) => {
        localStorage.clear();
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar;
        Cookies.set('avatar', avatar);
    }
}

const actions = {
    logout({commit}) {
        commit('LOGOUT')
    },
    setAvatar({commit},avatar){
        commit('SET_AVATAR',avatar);
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}